package com.hisan.freeride.home.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 创建时间 : 2017/11/16
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：无
 */
public class ViewHolder {
    private SparseArray<View> mViews = new SparseArray();
    private View convertView;
    private int mPosition;

    private ViewHolder(Context context, int position, int layoutId) {
        this.mPosition = position;
        this.convertView = View.inflate(context, layoutId, (ViewGroup)null);
        this.convertView.setTag(this);
    }

    public static ViewHolder getHolder(Context context, View convertView, int position, int layoutId) {
        if(convertView == null) {
            return new ViewHolder(context, position, layoutId);
        } else {
            ViewHolder holder = (ViewHolder)convertView.getTag();
            holder.mPosition = position;
            return holder;
        }
    }

    public <T extends View> T getView(int viewId) {
        View view = (View)this.mViews.get(viewId);
        if(view == null) {
            view = this.convertView.findViewById(viewId);
            this.mViews.put(viewId, view);
        }

        return (T) view;
    }

    public View getConvertView() {
        return this.convertView;
    }

    public int getmPosition() {
        return this.mPosition;
    }

    public ViewHolder setText(int viewId, String text) {
        TextView tv = (TextView)this.getView(viewId);
        tv.setText(text);
        return this;
    }
}
